package com.sinodata.bsm.cicp.finder.IIS6;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class WebSiteFinder extends IIS6Finder {

    private static final Logger logger = Logger.getLogger(WebSiteFinder.class);

    static final String cmdStr = "iissiteinfo.exe";
    static final String regStr = "\\[(\\S+)\\]\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)";

    /**
     * 根据一行匹配了确定规则的字符串,装备一个发现的资源
     * 
     * @param matchResult
     * @param attributes
     * @return
     */

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        Pattern patt = Pattern.compile(regStr);
        Matcher match = null;
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, cmdStr, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                match = patt.matcher(strLine);
                if (match.matches()) {
                    String[] info = new String[4];
                    list.add(info);
                    info[0] = match.group(1) + ip; //唯一标识是磁盘序列号
                    info[1] = match.group(1); //资源名就是盘符
                    info[2] = getHostName(ip) + " " + match.group(1);
                    info[3] = match.group(1);
                }
            }
            return list;
        } catch (Throwable t) {
            logger.error("failed to find iis6 website", t);
        }
        return null;
    }

}
